package com.shanliao.version;

import java.lang.reflect.Method;

import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.servlet.mvc.condition.RequestCondition;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import com.shanliao.version.annatation.OperationSubject;

public class CustomRequestMappingHandlerMapping2 extends RequestMappingHandlerMapping {
 
    @Override
    protected RequestCondition<OperationCondition> getCustomTypeCondition(Class<?> handlerType) {
    	OperationSubject apiVersion = AnnotationUtils.findAnnotation(handlerType, OperationSubject.class);
        return createCondition(apiVersion);
    }
 
    @Override
    protected RequestCondition<OperationCondition> getCustomMethodCondition(Method method) {
    	OperationSubject apiVersion = AnnotationUtils.findAnnotation(method, OperationSubject.class);
        return createCondition(apiVersion);
    }
     
    private RequestCondition<OperationCondition> createCondition(OperationSubject apiVersion) {
        return apiVersion == null ? null : new OperationCondition(apiVersion.value());
    }
}